<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input placeholder="用户名手机" v-model="form.username"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input placeholder="验证码" v-model="form.captcha">
        <template slot="append">
          <el-button @click="handleSendCaptcha">发送验证码</el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input placeholder="你的名字" v-model="form.nickname"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input placeholder="密码" type="password" v-model="form.password"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="checkPassword">
      <el-input placeholder="确认密码" type="password" v-model="form.checkPassword"></el-input>
    </el-form-item>

    <el-button class="submit" type="primary" @click="handleRegSubmit">注册</el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    // 校验手机号码
    const validateUsername = (rule,value,callback)=>{
      if(value===""){
        callback(new Error("手机号不能为空"))
      }else if(!(/^1[3|5|7|8][0-9]{9}/.test(value))){
        callback(new Error('手机号码格式错误'))
      }else{
        callback();
      }
    };
    //   确认密码 用element组件
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // 表单数据
      form: {
        username: "",
        captcha: "",
        nickname: "",
        password: "",
        checkPassword: ""
      },
      // 表单规则
      rules: {
        username: [
          // {
            //   required: true,
          //   message: "请输入用户名",
          //   trigger: "blur"
          // }
          {
            validator: validateUsername,
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          }
        ],
        checkPassword: [
          {
            validator: validatePass,
            trigger: "blur"
          }
        ],
        nickname: [
          {
            required: true,
            message: "请输入昵称",
            trigger: "blur"
          }
        ],
        captcha: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // 发送验证码
    async handleSendCaptcha() {
      // 手机号为空的情况
      if (!this.form.username) {
        // MessageBox 弹框 组件element
        // 模拟系统的消息提示框而实现的一套模态对话框组件，用于消息提示、确认消息和提交内容。
        // 调用$confirm方法即可打开消息提示
        this.$confirm("手机号码不能为空", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
        return;
      }
      //   手机号小于11位
      if (this.form.username.length !== 11) {
        this.$confirm("手机号码格式错误", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
        return;
      }
      //   发送ajax请求 请求验证码 这里是模拟
      // 拿到返回的code
      const code = await this.$store.dispatch('user/sendCaptcha',this.form.username)
      // 验证码弹框
      this.$message.success('模拟手机返回的验证码:' + code);
    },

    // 注册
    handleRegSubmit() {
      // console.log(this.form);
      this.$refs["form"].validate(async valid => {
        if (valid) {
          // 注册提交 解构props是自定义名字可变 将form表单中的checkPassword 去掉 后面的数据都在props中 
          const { checkPassword, ...props } = this.form;
          await this.$store.dispatch("user/register",props);
          // 跳转到首页
          this.$router.replace('/');
          // 弹框提示注册成功
          this.$message('注册成功');
        }
      });
    }
  }
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>